clear all
set more off
	
global dir = "C:\Users\mrueda\Documents\Emory\Papers\Networks_persistance\do_files\do_files_APSA21\post_JOP\Replication_BJPS\"	

cd "$dir"

use "Data\donor_level_persist_rep.dta",clear


gen treat=0
replace treat=1 if margin_victory>0&margin_victory~=.
replace treat=. if margin_victory==.	
gen treat_margin_victory=treat*margin_victory
	
keep if rank==1|rank==2
replace b5=. if b2!=0	


	texdoc close 
	cap erase "$dir/Tables/TableG1.tex"
	texdoc init "$dir/Tables/TableG1.tex", force

	tex \begin{table}[tbph]
	tex \caption{Effect of donating to an election winner on future donations (donor-level)}\label{tab:donations_d}
	tex \centering
	tex \begin{tabular}{l c c H} \hline
	tex Outcome:& Any race & Mayor & Other races \\
	tex & (1) & (2) & (3) \\ \hline
	tex & & & \\
	
	*Model 1
	foreach var in donate_15any b5{
	
		*Summary statistics for the mean
		quietly: regress `var' treat treat_margin_victory margin_victory , vce(cluster muni_code)
		quietly sum `var' if e(sample)
			local mean_`var' : di %5.3f r(mean)
			local sd_`var' : di %5.3f r(sd) 
		
		*Regressions
		rdrobust `var' margin_victory, p(1) vce(cluster muni_code) 
		
		*Local's for the table
		local bw_`var' : di %5.2f `e(h_l)'
		local ser_`var' = round(`e(se_tau_rb)',0.001)
		local Neff_`var' = `e(N_h_l)'+`e(N_h_r)'
		local N_`var' = `e(N)'
		local poly_`var' = `e(p)'
		local beta1_`var' : di %5.3f `e(tau_cl)'
		local beta2_`var' : di %5.3f `e(tau_bc)'

		*Confidence intervals
			local ser1_`var' : di %5.3f `e(ci_l_rb)'
			local ser2_`var' : di %5.3f `e(ci_r_rb)'
			
/* HERE*/	local em1_`var' = (`beta1_`var''/`mean_`var'')*100 
			local em1_`var' : di %5.2f `em1_`var''
			
		*P-values
		local pval2_`var' : di %5.3f `e(pv_rb)'
		scalar pval2_`var' = e(pv_rb)
		
		regress `var' treat treat_margin_victory margin_victory , vce(cluster muni_code)

		local N_`var' : di %5.0f e(N)
		local R2_`var' : di %5.3f e(r2)

		matrix b = e(b)
		matrix v = e(V)
		matrix res=r(table)
		
		local b1_`var' : di %5.3f b[1,1]
		local se1_`var' : di %5.3f sqrt(v[1,1])
		local p_v_`var' :di %5.3f res[4,1]
		local uci_`var': di %5.3f res[6,1]
		local lci_`var': di %5.3f res[5,1]
	}	
	
	*Continue table
	tex \multicolumn{3}{l}{Local linear}&\\
	tex Electoral victory & `beta1_donate_15any' & `beta1_b5' & `beta1_b2b' \\
	tex \ \ \ \ Robust p-value & `pval2_donate_15any' & `pval2_b5' & `pval2_b2b' \\
	tex \ \ \ \ CI 95\%  & [`ser1_donate_15any',`ser2_donate_15any'] & [`ser1_b5',`ser2_b5'] & [`ser1_b2b',`ser2_b2b'] \\
	tex & & & \\
	
	tex \multicolumn{3}{l}{Parametric (linear)}&\\
	tex Electoral victory & `b1_donate_15any' & `b1_b5' & `b1_b2b' \\
	tex \ \ \ \ p-value & `p_v_donate_15any' & `p_v_b5' & `p_v_b2b' \\
	tex \ \ \ \ CI 95\%  & [`lci_donate_15any',`uci_donate_15any'] & [`lci_b5',`uci_b5'] & [`lci_b2b',`uci_b2b'] \\
	tex & & & \\
	
	tex Observations & `N_donate_15any' & `N_b5' & `N_b2b' \\
	tex Bandwidth obs. & `Neff_donate_15any' & `Neff_b5' & `Neff_b2b' \\
	tex Mean & `mean_donate_15any' & `mean_b5' & `mean_b2b' \\
	*tex Effect Mean(\%) & `em1_donate_15any' & `em1_b5' & `em1_b2b' \\
	tex Bandwidth & `bw_donate_15any' & `bw_b5' & `bw_b2b' \\ \hline
	tex \end{tabular}
	tex \parbox{160mm}{ \footnotesize{
	tex Local linear estimates of average treatment effects at the cutoff estimated with triangular kernel weights and optimal MSE bandwidth. 95\% robust confidence intervals and robust p-values with clustering at the municipality level are computed following \cite{calonico_robust_2014}. Parametric linear model specification includes interaction of the treatment with running variable and running variable. Bandwidth obs. denotes the number of observations in the optimal MSE bandwidth.
	tex }
	tex }
	tex \end{table}
	cap texdoc close 
